package com.xkdx.guangguang;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;

import com.tencent.tauth.Tencent;
import com.weibo.sdk.android.sso.SsoHandler;
import com.xkdx.caipiao.R;
import com.xkdx.guangguang.fragment.brand.BrandInfoFragment;
import com.xkdx.guangguang.fragment.brand.branch.BranchFragment;
import com.xkdx.guangguang.fragment.home.homedetail.HomeDetailFragment;
import com.xkdx.guangguang.fragment.my.setup.ExchangeListFragment;
import com.xkdx.guangguang.fragment.my.setup.ExchangeShopFragment;
import com.xkdx.guangguang.fragment.newsinfo.DiscountFragment;
import com.xkdx.guangguang.fragment.newsinfo.NewsFragment;
import com.xkdx.guangguang.fragment.shopinfo.ShopInfoFragment;
import com.xkdx.guangguang.fragment.user.UserHomeFragment;
import com.xkdx.guangguang.fragment.user.UserLoginFragment;
import com.xkdx.guangguang.fragment.user.UserLoginSuccessFragment;
import com.xkdx.guangguang.module.statics.IConstants;
import com.xkdx.guangguang.pushserviceforSelf.PushForSelfInfo;

public class UserActivity extends ManagerActivity {

	UserHomeFragment userHomeFragment;
	// UserRegisteFragment userRegistFragment;
	private Tencent mTencent;
	private Tencent mbindTencent;// 绑定qq

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mTencent = Tencent.createInstance(IConstants.APP_ID,
				this.getApplicationContext());
		mbindTencent = Tencent.createInstance(IConstants.APP_ID,
				this.getApplicationContext());
		if (IConstants.isHasPush) {
			pushIntent();
		} else if (IConstants.selfInfo != null) {
			ProData(IConstants.selfInfo);
		} else {
			userHomeFragment = new UserHomeFragment();
			getSupportFragmentManager()
					.beginTransaction()
					.replace(R.id.fragment, userHomeFragment,
							"UserHomeFragment").commitAllowingStateLoss();
		}

		// Bundle b =getIntent().getExtras();
		// if (b != null) {
		// PushForSelfInfo pushInfo = null;
		// pushInfo = (PushForSelfInfo) b.getSerializable("pushself");
		// if (pushInfo != null && !("".equals(pushInfo.getObjectType()))) {
		// if (IConstants.selfInfo != null)
		// ProData(IConstants.selfInfo);
		// }
		// }
	}

	@Override
	protected void onStart() {
		IConstants.BottomBtnSelected = 5;
		if (getIntent() != null) {
			String name = getIntent().getStringExtra("fragment");
			if (name != null) {
				if (name.equals("ExchangeShopFragment")) {
					ExchangeShopFragment exchangeShopFragment = new ExchangeShopFragment();
					getSupportFragmentManager().beginTransaction()
							.replace(R.id.fragment, exchangeShopFragment)
							.addToBackStack(null).commitAllowingStateLoss();
				} else if (name.equals("UserLoginFragment")) {
					android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager()
							.beginTransaction();
					UserLoginFragment userLoginFragment = new UserLoginFragment();
					ft.replace(R.id.fragment, userLoginFragment,
							"userLoginFragment");
					ft.addToBackStack(null);
					ft.commitAllowingStateLoss();

				} else if (name.equals("ExchangeListFragment")) {
					ExchangeListFragment exchangeListFragment = new ExchangeListFragment();
					getSupportFragmentManager().beginTransaction()
							.replace(R.id.fragment, exchangeListFragment)
							.addToBackStack(null).commitAllowingStateLoss();
				}
				setIntent(null);
			}
		}
		super.onStart();
	}

	public UserHomeFragment getUserLoginFragment() {
		return userHomeFragment;
	}

	public Tencent getTencent() {
		return mTencent;
	}

	public Tencent getbindTencent() {
		return mbindTencent;
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		FragmentManager fm = getSupportFragmentManager();
		SsoHandler ssohandler = null;
		if (IConstants.LoginWhich == 2) { // 新浪登陆

			ssohandler = ((UserLoginFragment) fm
					.findFragmentByTag("userLoginFragment")).GetSsoHandler();
			if (ssohandler != null) {
				ssohandler.authorizeCallBack(requestCode, resultCode, data);
			}
		}

		if (IConstants.LoginWhich == 3) { // 腾讯登陆

			mTencent.onActivityResult(requestCode, resultCode, data);
		}

		if (IConstants.LoginWhich == 5) {

			mbindTencent.onActivityResult(requestCode, resultCode, data);
		}

		if (IConstants.LoginWhich == 4) { // 绑定新浪
			ssohandler = ((UserLoginSuccessFragment) fm
					.findFragmentByTag("userLoginSuccessFragment"))
					.GetBindSsoHandler();
			if (ssohandler != null) {
				ssohandler.authorizeCallBack(requestCode, resultCode, data);
			}
		}

	}

	@Override
	protected void onNewIntent(Intent intent) {
		setIntent(intent);
		super.onNewIntent(intent);
	}

	private void pushIntent() {

		int type = Integer.valueOf(IConstants.objectType);
		switch (type) {
		case 3:
			HomeDetailFragment detail = new HomeDetailFragment();
			Bundle b3 = new Bundle();
			b3.putString("infoID", IConstants.objectValue);
			detail.setArguments(b3);
			getSupportFragmentManager().beginTransaction()
					.replace(R.id.fragment, detail).addToBackStack(null)
					.commit();
			break;
		case 4:
			ShopInfoFragment info = new ShopInfoFragment();
			Bundle b4 = new Bundle();
			b4.putString("shopID", IConstants.objectValue);
			info.setArguments(b4);
			getSupportFragmentManager().beginTransaction()
					.replace(R.id.fragment, info).addToBackStack(null).commit();
			break;
		case 5:
			BranchFragment branchFragment = new BranchFragment();
			Bundle b5 = new Bundle();
			b5.putString("shopID", IConstants.objectValue);
			branchFragment.setArguments(b5);
			getSupportFragmentManager().beginTransaction()
					.replace(R.id.fragment, branchFragment)
					.addToBackStack(null).commit();
			break;
		case 6:
			BrandInfoFragment brandInfo = new BrandInfoFragment();
			Bundle b6 = new Bundle();
			b6.putString("brandID", IConstants.objectValue);
			brandInfo.setArguments(b6);
			getSupportFragmentManager().beginTransaction()
					.replace(R.id.fragment, brandInfo).addToBackStack(null)
					.commit();
			break;
		case 7:
			NewsFragment newsFragment = new NewsFragment();
			Bundle b7 = new Bundle();
			b7.putString("infoID", IConstants.objectValue);
			newsFragment.setArguments(b7);
			getSupportFragmentManager().beginTransaction()
					.replace(R.id.fragment, newsFragment).addToBackStack(null)
					.commit();

			break;
		case 8:
			DiscountFragment discountInfo = new DiscountFragment();
			Bundle b8 = new Bundle();
			b8.putString("infoID", IConstants.objectValue);
			discountInfo.setArguments(b8);
			getSupportFragmentManager().beginTransaction()
					.replace(R.id.fragment, discountInfo).addToBackStack(null)
					.commit();

			break;
		default:
			break;
		}
	}

	@Override
	protected void onResume() {

		super.onResume();

	}

	private void ProData(PushForSelfInfo pushInfo) {

		int type = Integer.valueOf(pushInfo.getObjectType());
		String value = pushInfo.getObjectValue();
		switch (type) {
		case 3:
			HomeDetailFragment detail = new HomeDetailFragment();
			Bundle b3 = new Bundle();
			b3.putString("infoID", value);
			detail.setArguments(b3);
			getSupportFragmentManager().beginTransaction()
					.replace(R.id.fragment, detail).addToBackStack(null)
					.commitAllowingStateLoss();
			break;
		case 4:
			ShopInfoFragment info = null;
			if (info == null) {
				info = new ShopInfoFragment();
			}
			Bundle b4 = new Bundle();
			b4.putString("shopID", value);
			info.setArguments(b4);
			getSupportFragmentManager().beginTransaction()
					.replace(R.id.fragment, info).addToBackStack(null)
					.commitAllowingStateLoss();
			break;
		case 5:
			BranchFragment branchFragment = new BranchFragment();
			Bundle b5 = new Bundle();
			b5.putString("shopID", value);
			branchFragment.setArguments(b5);
			getSupportFragmentManager().beginTransaction()
					.replace(R.id.fragment, branchFragment)
					.addToBackStack(null).commitAllowingStateLoss();
			break;
		case 6:
			BrandInfoFragment brandInfo = new BrandInfoFragment();
			Bundle b6 = new Bundle();
			b6.putString("brandID", value);
			brandInfo.setArguments(b6);
			getSupportFragmentManager().beginTransaction()
					.replace(R.id.fragment, brandInfo).addToBackStack(null)
					.commitAllowingStateLoss();
			break;
		case 7:
			NewsFragment newsFragment = new NewsFragment();
			Bundle b7 = new Bundle();
			b7.putString("infoID", value);
			newsFragment.setArguments(b7);
			getSupportFragmentManager().beginTransaction()
					.replace(R.id.fragment, newsFragment).addToBackStack(null)
					.commitAllowingStateLoss();

			break;
		case 8:
			DiscountFragment discountInfo = new DiscountFragment();
			Bundle b8 = new Bundle();
			b8.putString("infoID", value);
			discountInfo.setArguments(b8);
			getSupportFragmentManager().beginTransaction()
					.replace(R.id.fragment, discountInfo).addToBackStack(null)
					.commitAllowingStateLoss();

			break;
		default:
			break;

		}
		IConstants.selfInfo = null;
		// IConstants.isFromSelf = false;
	}

}
